<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\common\model\User;

class Node extends Base
{
    public function index1(): string
    {
        $map['platoon_id'] = 1;
        $keyword = $this->request->get('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $id = $this->request->get('id');
            if ($id) {
                $map['id'] = $id;
            } else {
                $map['id'] = User::where('platoon_id', 1)->where('is_sub', 1)->value('id') ?? 0;
            }
        }

        $field = 'id,code,nickname,mobile,node_id,position,is_active,active_time,level_id,num_1,num_2,total_1,total_2,surplus_1,surplus_2,master_id';
        $first = User::with(['level', 'mat'])->field($field)->where($map)->find();
        $new = $childSon = [1 => [], 2 => []];
        if ($first) {
            foreach ($new as $key => $item) {
                $item = User::with(['level', 'mat'])->field($field)->where(['node_id' => $first->id, 'position' => $key])->find();
                foreach ($childSon as $k => $v) {
                    if (!empty($item)) {
                        $v = User::with(['level', 'mat'])->field($field)->where(['node_id' => $item['id'], 'position' => $k])->find();
                        $childSon[$k] = $v ? $v->toArray() : [];
                    }
                }
                $new[$key] = $item ? $item->toArray() : [];
                $new[$key]['child'] = $childSon;
            }
        }

        View::assign([
            'color' => ['deep', 'red', 'blue'],
            'first' => $first,
            'child' => $new
        ]);

        return View::fetch();
    }

    public function index2(): string
    {
        $map['platoon_id'] = 2;
        $keyword = $this->request->get('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $id = $this->request->get('id');
            if ($id) {
                $map['id'] = $id;
            } else {
                $map['id'] = User::where('platoon_id', 2)->where('is_sub', 1)->value('id') ?? 0;
            }
        }

        $field = 'id,code,nickname,mobile,node_id,position,is_active,active_time,level_id,num_1,num_2,total_1,total_2,surplus_1,surplus_2,master_id';
        $first = User::with(['level', 'mat'])->field($field)->where($map)->find();
        $new = $childSon = [1 => [], 2 => []];
        if ($first) {
            foreach ($new as $key => $item) {
                $item = User::with(['level', 'mat'])->field($field)->where(['node_id' => $first->id, 'position' => $key])->find();
                foreach ($childSon as $k => $v) {
                    if (!empty($item)) {
                        $v = User::with(['level', 'mat'])->field($field)->where(['node_id' => $item['id'], 'position' => $k])->find();
                        $childSon[$k] = $v ? $v->toArray() : [];
                    }
                }
                $new[$key] = $item ? $item->toArray() : [];
                $new[$key]['child'] = $childSon;
            }
        }
        View::assign([
            'color' => ['deep', 'red', 'blue'],
            'first' => $first,
            'child' => $new
        ]);

        return View::fetch();
    }

    public function index3(): string
    {
        $map['platoon_id'] = 3;
        $keyword = $this->request->get('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $id = $this->request->get('id');
            if ($id) {
                $map['id'] = $id;
            } else {
                $map['id'] = User::where('platoon_id', 3)->where('is_sub', 1)->value('id') ?? 0;
            }
        }

        $field = 'id,code,nickname,mobile,node_id,position,is_active,active_time,level_id,num_1,num_2,total_1,total_2,surplus_1,surplus_2,master_id';
        $first = User::with(['level', 'mat'])->field($field)->where($map)->find();
        $new = $childSon = [1 => [], 2 => []];
        if ($first) {
            foreach ($new as $key => $item) {
                $item = User::with(['level', 'mat'])->field($field)->where(['node_id' => $first->id, 'position' => $key])->find();
                foreach ($childSon as $k => $v) {
                    if (!empty($item)) {
                        $v = User::with(['level', 'mat'])->field($field)->where(['node_id' => $item['id'], 'position' => $k])->find();
                        $childSon[$k] = $v ? $v->toArray() : [];
                    }
                }
                $new[$key] = $item ? $item->toArray() : [];
                $new[$key]['child'] = $childSon;
            }
        }
        View::assign([
            'color' => ['deep', 'red', 'blue'],
            'first' => $first,
            'child' => $new
        ]);

        return View::fetch();
    }
}